<template>
  <div>
    <el-row>
      <!-- span 自己的宽带占12份 左侧边距6份 右侧也剩下6份 所以居中 -->
      <el-col :span="12" :offset="6">
        <div class="content">
          <h1>欢迎登录</h1>
          <!-- model 表单的数据 -->
          <!-- rules 校验的规则 -->
          <!-- label-width 文字的宽度 -->
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
              <!-- <el-input placeholder="请输入内容" v-model="input1">
                <template slot="prepend">Http://</template>
              </el-input> -->
              <!-- 这里可以将input的文字替换成小图标 slot="prepend" 是具名插槽-->
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">登录</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { userLogin } from "../apis";
export default {
  data() {
    return {
      // 整个表单的数据
      ruleForm: {
        username: "10086",
        password: "123456",
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请填写密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 点击按钮 提交表单
    submitForm() {
      // this.$refs.ruleForm 获取表单组件
      // validate 组件中自带的检验函数
      // valid校验的结果
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 成功
          let res = await userLogin(this.ruleForm);
          console.log(res);
          this.$store.commit('saveUserInfo', res.list)
          this.$router.push("/");
        } else {
          // 失败
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.content {
  height: 400px;
  margin-top: 100px;
  border: 1px solid #333;
}
</style>